<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          type="primary"
          :loading="exportLoading"
          @click="exportExcel()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>

      <template v-slot:projectEleStatus="scope">
        <span class="badge" :class="`badge-${scope.row.projectEleStatusStyle}`">
          {{ scope.row.projectEleStatusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="viewVisible"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="viewActive">
        <el-tab-pane label="项目信息" name="first">
          <info-view :related-data="viewData"></info-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter">
          <letter-view :query-id="queryId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="放款通知书">
          <loan-notice :query-id="queryId"></loan-notice>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { enumGuatype } from '@/credit/plugins/enumTown'
import { enumRenewalStatus } from '@/credit/plugins/enum'

import { paramsMixins } from '@/bank/views/business/28/normal/mixins'
import { exportTable } from '@/bank/plugins/export'

import SelectAddress from '@/components/select/SelectAddress'

import LoanNotice from '@/credit/views/county/unionProvince/components/LoanNotice'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import InfoView from '@/credit/views/city/bus/normal/Info'

export const postloanTableColumns = [
  {
    attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
    process: (v, row) => ({
      attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
    }),
  },
  { attrs: { label: '业务类型', prop: 'guaType', align: 'center' } },
  { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
  { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
  { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
  { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' } },
  { attrs: { label: '在保余额', prop: 'left_funds', align: 'center' } },
  { attrs: { label: '到期时间', prop: 'endDate', align: 'center' } },
  { attrs: { label: '贷后状态', prop: 'renewalStatus', align: 'center' } },
]

export default {
  components: {
    SelectAddress,
    LetterView,
    LoanNotice,
    InfoView,
  },
  mixins: [paramsMixins],
  props: {},
  data() {
    return {
      loading: false,
      exportLoading: false,

      params: {
        index: 1,
        size: 10,
        'loan_approval_status.eq': 3,
      },
      order: {
        load_end_date: 'DESC',
      },
      tableName: 'customer_related',
      tableData: [],
      tableCount: 0,
      tableColumns: [
        ...postloanTableColumns,
        {
          attrs: { label: '操作', align: 'center', minWidth: 120 },
          operations: [
            {
              content: '贷后详情',
              icon: 'fa fa-edit',
              event: this.handleEdit,
            },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],

      // 查看
      viewActive: 'first',
      viewVisible: false,
      viewData: {},
    }
  },
  computed: {
    ...mapGetters(['org']),
  },
  watch: {
    status: {
      handler: function (val) {
        if (+val === 1) {
          this.params['load_beh_status.neq'] = 6
          this.params['load_end_date.lte'] = this.$utils.dateFormat(
            +new Date() + 60 * 8.64e7,
            'yyyy-MM-dd'
          )
        } else {
          this.params['load_beh_status.neq'] = ''
          this.params['load_end_date.lte'] = ''
        }
        this.getData()
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    getParams() {
      return {
        ...this.params,
        ...this.baseParams,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData({
          table: this.tableName,
          params,
          order: this.order || {},
        })
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.tableCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.endDate = this.$utils.dateFormat(item.load_end_date, 'yyyy-MM-dd')
      item.renewalStatus = enumRenewalStatus[item.load_beh_status] || '-'
      return item
    },
    exportExcel() {
      this.exportLoading = true
      exportTable({
        columns: this.tableColumns,
        table: this.tableName,
        params: this.getParams(),
        order: this.order,
        adapter: this.dataAdapter,
        filename: '审批登记',
      }).finally(() => {
        this.exportLoading = false
      })
    },
    handleEdit(row = {}) {
      this.$router.push({
        name: 'missionAfter',
        query: {
          id: row.customer_related_id,
          farmid: row.customer_id,
        },
      })
    },
    hanleView(row = {}) {
      this.viewData = row
      this.queryId = this.viewData.customer_related_id
      this.viewVisible = true
    },
    beforeClose() {
      this.viewVisible = false
      setTimeout(() => (this.viewActive = 'first'), 200)
    },
  },
}
</script>
<style lang="scss" scoped></style>
